<?php
/**
 * Created by PhpStorm.
 * Script Name: Onmessage.php
 * Create: 12/25/21 9:43 PM
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\bot\controller;

use app\admin\model\Bot;
use app\admin\model\BotGroupmember;
use app\admin\model\BotMember;
use app\bot\handler\vlw\EventFrieneVerify;
use app\bot\handler\vlw\EventPrivateChat;
use app\bot\handler\vlw\Login;
use app\common\controller\BaseCtl;
use ky\Bot\Vlw;
use ky\Bot\Wxwork;
use ky\Helper;
use ky\Jtt;
use ky\Logger;

class Api extends BaseCtl
{

    /**
     * @var Bot
     */
    protected $botM;
    /**
     * @var BotMember
     */
    protected $memberM;
    /**
     * @var BotGroupmember
     */
    protected $groupMemberM;
    protected $bot;
    /**
     * @var Vlw|Wxwork
     */
    protected $botClient;
    protected $fromWxid = '';
    protected $botWxid = '';
    protected $groupWxid = '';
    protected $content;
    protected $group;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        set_time_limit(0);
        Helper::$ajax = $this->getAjax();
    }

    /**
     * 全局参数
     * @throws \think\exception\DbException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    protected function initData(){
        if(request()->isPost()) {
            $this->botM = new Bot();
            $this->memberM = new BotMember();
            $this->groupMemberM = new BotGroupmember();
            sleep(1); //间隔两次请求做个随机等待

            $this->botWxid = !empty(Helper::$ajax['content']['robot_wxid']) ? Helper::$ajax['content']['robot_wxid'] : Helper::$ajax['content']['Wxid'];
            $this->bot = $this->getBot($this->botWxid);
            $this->botClient = $this->botM->getRobotClient($this->bot);
            //Logger::error(json_encode(Helper::$ajax, JSON_UNESCAPED_UNICODE));
            $this->content = Helper::$ajax['content'];
            $this->fromWxid = empty($this->content['from_wxid']) ? $this->botWxid : $this->content['from_wxid'];
        }else{
            Logger::error('Request invalid!');
            exit(0);
        }
    }

    /**
     * vlw框架
     * tip:
     * 1.机器人对某个好友的私聊不会有回调
     * 2.机器人在群里发的情况下，from_wxid和from_group 都为空
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function vlw(){
        $event = Helper::$ajax['Event'];

        $class = '\\app\\bot\\handler\\vlw\\' . ucfirst($event);
        if(! class_exists($class)){
            Logger::error("class: " . $class . " not exists!");
            exit(0);
        }
        /**
         * @var $handler Login|EventFrieneVerify|EventPrivateChat
         */
        $handler = new $class();
        $handler->initData();
        $handler->handle();
    }

    /**
     * @param string $uin
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\exception\DbException Author: fudaoji<fdj@kuryun.cn>
     */
    private function getBot($uin = ''){
        $map = ['uin' => $uin, 'alive' => 1];
        if(Helper::$ajax['Event'] == \app\constants\Bot::EVENT_LOGIN){
            unset($map['alive']);
        }
        if(! $bot = $this->botM->getOneByMap($map)) {
            //Logger::error('Bot not exists or not logged in: ' . $uin);
            exit(0);
        }
        return $bot;
    }

    /**
     * @param string $nickname
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\exception\DbException Author: fudaoji<fdj@kuryun.cn>
     */
    protected function getGroupMemberByNickname($nickname=''){
        $group = $this->memberM->getOneByMap(
            ['uin' => $this->bot['uin'], 'wxid' => $this->groupWxid],
            ['id']
        );
        return $this->groupMemberM->getOneByMap(['nickname' => $nickname, 'group_id' => $group['id']]);
    }
}